home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / packer / gzip-1.2.2 / znew.in < prev   
Encoding:
Text File  |  1993-06-09  |  3.6 KB  |  155 lines

  1. :
  2. #!/bin/sh
  3.  
  4. PATH="BINDIR:$PATH"; export PATH
  5. check=0
  6. pipe=0
  7. opt=
  8. files=
  9. keep=0
  10. res=0
  11. old=0
  12. new=0
  13. block=1024
  14. # block is the disk block size (best guess, need not be exact)
  15.  
  16. warn="(does not preserve modes and timestamp)"
  17. tmp=/tmp/zfoo.$$
  18. echo hi > $tmp.1
  19. echo hi > $tmp.2
  20. if test -z "`(${CPMOD-cpmod} $tmp.1 $tmp.2) 2>&1`"; then
  21.   cpmod=${CPMOD-cpmod}
  22.   warn=""
  23. fi
  24.  
  25. if test -z "$cpmod" && ${TOUCH-touch} -r $tmp.1 $tmp.2 2>/dev/null; then
  26.   cpmod="${TOUCH-touch}"
  27.   cpmodarg="-r"
  28.   warn="(does not preserve file modes)"
  29. fi
  30.  
  31. # check if GZIP env. variable uses -S or --suffix
  32. gzip -q $tmp.1
  33. ext=`echo $tmp.1* | sed "s|$tmp.1||"`
  34. rm -f $tmp.[12]*
  35. if test -z "$ext"; then
  36.   echo znew: error determining gzip extension
  37.   exit 1
  38. fi
  39. if test "$ext" = ".Z"; then
  40.   echo znew: cannot use .Z as gzip extension.
  41.   exit 1
  42. fi
  43.  
  44. A=
  45. fileno=0
  46.  
  47. for arg
  48. do
  49.   case "$arg" in
  50.   -*)     opt="$opt $arg";;
  51.    *)     fileno=`expr $fileno + 1`
  52.           eval A$fileno=\$arg
  53.           A="$A \"\$A$fileno\""
  54.       ;;
  55.   esac
  56. done
  57.  
  58. if test $fileno -eq 0; then
  59.   echo "recompress .Z files into $ext (gzip) files"
  60.   echo usage: `echo $0 | sed 's,^.*/,,'` "[-tv9KP]" file.Z...
  61.   echo "  -t tests the new files before deleting originals"
  62.   echo "  -v be verbose"
  63.   echo "  -9 use the slowest compression method (optimal compression)"
  64.   echo "  -K keep a .Z file when it is smaller than the $ext file"
  65.   echo "  -P use pipes for the conversion $warn"
  66.   exit 1
  67. fi
  68.  
  69. eval set "$A"                # the files are now in $1, $2, ...
  70.  
  71. opt=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
  72. case "$opt" in
  73.   *t*) check=1; opt=`echo "$opt" | sed 's/t//g'`
  74. esac
  75. case "$opt" in
  76.   *K*) keep=1; opt=`echo "$opt" | sed 's/K//g'`
  77. esac
  78. case "$opt" in
  79.   *P*) pipe=1; opt=`echo "$opt" | sed 's/P//g'`
  80. esac
  81. if test -n "$opt"; then
  82.   opt="-$opt"
  83. fi
  84.  
  85. for i do
  86.   n=`echo $i | sed 's/.Z$//'`
  87.   if test ! -f "$n.Z" ; then
  88.     echo $n.Z not found
  89.     res=1; continue
  90.   fi
  91.   test $keep -eq 1 && old=`wc -c < "$n.Z"`
  92.   if test $pipe -eq 1; then
  93.     if gzip -d < "$n.Z" | gzip $opt > "$n$ext"; then
  94.       # Copy file attributes from old file to new one, if possible.
  95.       test -n "$cpmod" && $cpmod $cpmodarg "$n.Z" "$n$ext" 2> /dev/null
  96.     else
  97.       echo error while recompressing $n.Z
  98.       res=1; continue
  99.     fi
  100.   else
  101.     if test $check -eq 1; then
  102.       if cp -p "$n.Z" "$n.$$" 2> /dev/null || cp "$n.Z" "$n.$$"; then
  103.     :
  104.       else
  105.     echo cannot backup "$n.Z"
  106.         res=1; continue
  107.       fi
  108.     fi
  109.     if gzip -d "$n.Z"; then
  110.       :
  111.     else
  112.       test $check -eq 1 && mv "$n.$$" "$n.Z"
  113.       echo error while uncompressing $n.Z
  114.       res=1; continue
  115.     fi
  116.     if gzip $opt "$n"; then
  117.       :
  118.     else
  119.       if test $check -eq 1; then
  120.     mv "$n.$$" "$n.Z" && rm -f "$n"
  121.         echo error while recompressing $n
  122.       else
  123.     # compress $n  (might be dangerous if disk full)
  124.         echo error while recompressing $n, left uncompressed
  125.       fi
  126.       res=1; continue
  127.     fi
  128.   fi
  129.   test $keep -eq 1 && new=`wc -c < "$n$ext"`
  130.   if test $keep -eq 1 -a `expr \( $old + $block - 1 \) / $block` -lt \
  131.                          `expr \( $new + $block - 1 \) / $block`; then
  132.     if test $pipe -eq 1; then
  133.       rm -f "$n$ext"
  134.     elif test $check -eq 1; then
  135.       mv "$n.$$" "$n.Z" && rm -f "$n$ext"
  136.     else
  137.       gzip -d "$n$ext" && compress "$n" && rm -f "$n$ext"
  138.     fi
  139.     echo "$n.Z smaller than $n$ext -- unchanged"
  140.  
  141.   elif test $check -eq 1; then
  142.     if gzip -t "$n$ext" ; then
  143.       rm -f "$n.$$" "$n.Z"
  144.     else
  145.       test $pipe -eq 0 && mv "$n.$$" "$n.Z"
  146.       rm -f "$n$ext"
  147.       echo error while testing $n$ext, $n.Z unchanged
  148.       res=1; continue
  149.     fi
  150.   elif test $pipe -eq 1; then
  151.     rm -f "$n.Z"
  152.   fi
  153. done
  154. exit $res
  155.